package com.example.xunai.dto.response.user;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TokenStatsResponse {
    private Long userId;
    private Integer totalInputTokens;
    private Integer totalOutputTokens;
    private Integer totalTokens;
    private Integer modelCount;
    private List<Map<String, Object>> dailyTrend;
    private List<Map<String, Object>> modelDistribution;
    private Map<String, Object> usageSummary;

    public static TokenStatsResponse of(Long userId, Integer totalInputTokens, Integer totalOutputTokens,
                                        Integer modelCount, List<Map<String, Object>> dailyTrend,
                                        List<Map<String, Object>> modelDistribution) {
        int totalTokens = totalInputTokens + totalOutputTokens;

        Map<String, Object> usageSummary = Map.of(
                "inputPercentage", calculatePercentage(totalInputTokens, totalTokens),
                "outputPercentage", calculatePercentage(totalOutputTokens, totalTokens),
                "avgDailyTokens", calculateAverageDailyTokens(dailyTrend, totalTokens),
                "mostUsedModel", findMostUsedModel(modelDistribution)
        );

        return TokenStatsResponse.builder()
                .userId(userId)
                .totalInputTokens(totalInputTokens)
                .totalOutputTokens(totalOutputTokens)
                .totalTokens(totalTokens)
                .modelCount(modelCount)
                .dailyTrend(dailyTrend)
                .modelDistribution(modelDistribution)
                .usageSummary(usageSummary)
                .build();
    }

    private static Double calculatePercentage(Integer part, Integer total) {
        if (total == null || total == 0) return 0.0;
        return Math.round((double) part / total * 10000.0) / 100.0;
    }

    private static Integer calculateAverageDailyTokens(List<Map<String, Object>> dailyTrend, Integer totalTokens) {
        if (dailyTrend == null || dailyTrend.isEmpty()) return 0;
        return totalTokens / dailyTrend.size();
    }

    private static String findMostUsedModel(List<Map<String, Object>> modelDistribution) {
        if (modelDistribution == null || modelDistribution.isEmpty()) return "unknown";

        return modelDistribution.stream()
                .max((m1, m2) -> {
                    Integer t1 = (Integer) m1.getOrDefault("totalTokens", 0);
                    Integer t2 = (Integer) m2.getOrDefault("totalTokens", 0);
                    return t1.compareTo(t2);
                })
                .map(m -> (String) m.get("model"))
                .orElse("unknown");
    }

    public Double getInputOutputRatio() {
        if (totalOutputTokens == null || totalOutputTokens == 0) return 0.0;
        return Math.round((double) totalInputTokens / totalOutputTokens * 100.0) / 100.0;
    }
}